package com.jdbc.connection;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/**
 * @author wzm
 * @create 2023-10-31 9:33
 * project_name:web2310
 */
//测试java连接mysql
public class Conn02 {
    public static void main(String[] args) throws Exception {
        //从文件中获取连接信息
        InputStream is = Conn02.class.getClassLoader().getResourceAsStream("jdbc.properties");

        //下面这种方式是能用于简单此时 在服务器中得到的结果为null
        //InputStream is2 = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");


        //准备好Properties
        Properties properties = new Properties();
        properties.load(is);
        System.out.println("properties = " + properties);


        //根据key获取value
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driverClass = properties.getProperty("driverClass");


        //1.注册驱动 将驱动类加载到java虚拟机中
        Class.forName(driverClass);

        //2.通过驱动管理器获取mysql的连接对象
        Connection conn = DriverManager.getConnection(url, user, password);


        if(conn != null){
            System.out.println("获取连接成功");
        }else{
            System.out.println("获取连接失败");
        }

    }
}
